/**
 * 项目反馈控制器
 */
const BaseProjectController = require('./base_project_controller.js');
const timeUtil = require('../../../framework/helpers/time_util.js');
const dataUtil = require('../../../framework/helpers/data_util.js');
const projectUtil = require('../../../framework/helpers/project_util.js');

const FeedbackModel = require('../config/models_config').project_feedback_model;
const ProjectModel = require('../config/models_config').project_model;

class FeedbackController extends BaseProjectController {

	/** 获取反馈列表 */
	async getFeedbackList() {
		// 拦截未登录请求
		await this.isLogin();

		// 获取参数
		const { projectId } = this.getParams();

		// 获取用户信息
		const userId = this.getUserId();

		// 构建查询条件
		let where = {
			FEEDBACK_PROJECT_ID: projectId
		};

		// 查询数据
		let result = await FeedbackModel.getList(where, 'FEEDBACK_ADD_TIME desc');

		// 处理结果
		for (let k in result) {
			result[k].FEEDBACK_ADD_TIME_DESC = timeUtil.timestamp2Time(result[k].FEEDBACK_ADD_TIME);
		}

		return result;
	}

	/** 获取反馈详情 */
	async getFeedbackDetail() {
		// 拦截未登录请求
		await this.isLogin();

		// 获取参数
		const { id } = this.getParams();

		// 获取用户信息
		const userId = this.getUserId();

		// 查询数据
		let result = await FeedbackModel.getOne(id);
		if (!result) {
			this.AppError('反馈不存在或已被删除');
		}

		// 处理时间
		result.FEEDBACK_ADD_TIME_DESC = timeUtil.timestamp2Time(result.FEEDBACK_ADD_TIME);

		return result;
	}

	/** 添加反馈 */
	async addFeedback() {
		// 拦截未登录请求
		await this.isLogin();

		// 获取参数
		const {
			projectId,
			title,
			content,
			type,
			priority,
			customerId,
			customerName,
			images = [],
			files = []
		} = this.getParams();

		// 参数校验
		if (!projectId) {
			this.AppError('缺少项目参数');
		}

		if (!title) {
			this.AppError('请填写反馈标题');
		}

		if (!content) {
			this.AppError('请填写反馈内容');
		}

		// 获取用户信息
		const user = await this.getUser();
		const userId = this.getUserId();

		// 获取项目信息
		const project = await ProjectModel.getOne(projectId);
		if (!project) {
			this.AppError('项目不存在或已被删除');
		}

		// 构造数据
		const data = {
			FEEDBACK_PROJECT_ID: projectId,
			FEEDBACK_TITLE: title,
			FEEDBACK_CONTENT: content,
			FEEDBACK_TYPE: type || 1,
			FEEDBACK_PRIORITY: priority || 2,
			FEEDBACK_CUSTOMER_ID: customerId || '',
			FEEDBACK_CUSTOMER_NAME: customerName || '',
			FEEDBACK_IMAGES: images,
			FEEDBACK_FILES: files,
			FEEDBACK_STATUS: 1,
			FEEDBACK_SUBMITTER_ID: userId,
			FEEDBACK_SUBMITTER_NAME: user.USER_NAME,
			FEEDBACK_ADD_TIME: timeUtil.time()
		};

		// 插入数据库
		const id = await FeedbackModel.insert(data);

		// 返回结果
		return {
			id
		};
	}

	/** 更新反馈 */
	async updateFeedback() {
		// 拦截未登录请求
		await this.isLogin();

		// 获取参数
		const {
			id,
			title,
			content,
			type,
			priority,
			customerId,
			customerName,
			status,
			images = [],
			files = []
		} = this.getParams();

		// 参数校验
		if (!id) {
			this.AppError('缺少反馈ID参数');
		}

		// 查询原数据
		const oldData = await FeedbackModel.getOne(id);
		if (!oldData) {
			this.AppError('反馈不存在或已被删除');
		}

		// 获取用户信息
		const user = await this.getUser();
		const userId = this.getUserId();

		// 构造更新数据
		const data = {};

		if (title) data.FEEDBACK_TITLE = title;
		if (content) data.FEEDBACK_CONTENT = content;
		if (type) data.FEEDBACK_TYPE = type;
		if (priority) data.FEEDBACK_PRIORITY = priority;
		if (customerId) data.FEEDBACK_CUSTOMER_ID = customerId;
		if (customerName) data.FEEDBACK_CUSTOMER_NAME = customerName;
		if (status) data.FEEDBACK_STATUS = status;
		if (images) data.FEEDBACK_IMAGES = images;
		if (files) data.FEEDBACK_FILES = files;

		// 添加更新者信息
		data.FEEDBACK_UPDATE_TIME = timeUtil.time();
		data.FEEDBACK_UPDATER_ID = userId;
		data.FEEDBACK_UPDATER_NAME = user.USER_NAME;

		// 更新数据库
		await FeedbackModel.edit(id, data);

		return {
			id
		};
	}
}

module.exports = FeedbackController; 